import {applicationFormUrlEncodedHeader, applicationJsonHeader, doGet, doPost} from "@/net/axios-request.ts";
import {userTokenHeader} from "@/utils/auth-utils.ts";
import type {PagedData} from "@/net/Result.ts";

export interface Post {
    postId: number,
    userId: number,
    categoryId: number,
    title: string,
    content: string,
    attachments: string,
    isTop: number,
    isStar: number,
    viewCount: number,
    createdAt: string
}

export function getEmptyPost(): Post {
    return {
        attachments: "",
        categoryId: 0,
        content: "",
        createdAt: "",
        isStar: 0,
        isTop: 0,
        postId: 0,
        title: "",
        userId: 0,
        viewCount: 0
    }
}

export interface PostEditDTO {
    postId: number,
    title: string,
    content: string,
    category: number
}

export async function listPosts(page: number) {
    return (await doGet<PagedData<Post>>("/api/post/list", {}, { page: page })).data
}

export async function listLatestPosts(page: number) {
    return (await doGet<PagedData<Post>>("/api/post/listLatest", {}, { page: page })).data
}

export async function listStarPosts(page: number) {
    return (await doGet<PagedData<Post>>("/api/post/listStar", {}, { page: page })).data
}

export async function listTopPosts(page: number) {
    return (await doGet<PagedData<Post>>("/api/post/listTop", {}, { page: page })).data
}

export async function listHotPosts(page: number) {
    return (await doGet<PagedData<Post>>("/api/post/listHot", {}, { page: page })).data
}

export async function listPostsByCat(page: number, catId: number) {
    return (await doGet<PagedData<Post>>("/api/post/listByCat", {}, { page: page, catId: catId })).data
}

export async function getPostDetails(postId: number) {
    return (await doGet<Post>("/api/post/details", {...userTokenHeader()}, { postId: postId })).data
}

export async function publishPost(dto: PostEditDTO) {
    return await doPost("/api/post/publish", {...userTokenHeader(), ...applicationJsonHeader}, {...dto})
}

export async function setPostTopStatus(postId: number, value: boolean) {
    return await doPost("/api/post/setTop", {...userTokenHeader(), ...applicationFormUrlEncodedHeader}, { postId: postId, value: value })
}

export async function setPostStarStatus(postId: number, value: boolean) {
    return await doPost("/api/post/setStar", {...userTokenHeader(), ...applicationFormUrlEncodedHeader}, { postId: postId, value: value })
}

export async function increaseViewCount(postId: number) {
    return await doPost("/api/post/increaseViewCount", {...userTokenHeader(), ...applicationFormUrlEncodedHeader}, { postId: postId })
}

export async function deletePost(postId: number) {
    return await doPost("/api/post/delete", {...userTokenHeader(), ...applicationFormUrlEncodedHeader}, { postId: postId })
}